import { GlobalHelp, DynamicInstanceConfig } from "@ibiz/dynamic-model-api";
import { AppServiceBase, AppModelService, Http } from "ibiz-core";
import { AppCenterService } from "ibiz-vue";
import i18n from "@/locale";
import { handleLocaleMap } from "@/locale/local-util";

export class AuthGuardBase {
  /**
   * @description 初始化应用服务
   * @param {*} router
   * @return {*}  {Promise<void>}
   * @memberof PreviewUtil
   */
  async initAppService(router: any): Promise<void> {
    AppServiceBase.getInstance().setAppStore(router.app.$store);
    AppServiceBase.getInstance().setI18n(i18n);
    AppServiceBase.getInstance().setRouter(router);
    const service = new AppModelService();
    const Environment = AppServiceBase.getInstance().getAppEnvironment();
    await GlobalHelp.install(service, async (strPath: string, config: DynamicInstanceConfig) => {
      let url: string = '';
      if (Environment.bDynamic) {
        url = `${Environment.remoteDynaPath}${strPath}`;
        if (config) {
          url += `?srfInstTag=${config.instTag}&srfInstTag2=${config.instTag2}`;
        }
      } else {
        url = `/model${strPath}`;
      }
      try {
        const result: any = await Http.getInstance().get(url);
        return result.data ? result.data : null;
      } catch (error) {
        return null;
      }
    }, { lang: Environment?.IsEnableMultiLan ? handleLocaleMap(i18n.locale) : '' });
    AppServiceBase.getInstance().setAppModelDataObject(service.app);
    AppCenterService.getInstance(router.app.$store);
  }
}